<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.buvaka.features.*"%>
<%@page import="java.net.UnknownHostException"%>
<%@page import="com.buvaka.UpdateGeoJsonFile"%>
<%@ page import = "com.mongodb.*" %>
<%@ page import="org.bson.types.ObjectId" %>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title>Добавить объект</title>
</head>
<body>

<%
    try{
        MongoClient client = new MongoClient();
        DB db = client.getDB("gis");
        DBCollection categories = db.getCollection("categories");
        request.setAttribute("client", client);
        request.setAttribute("categories", categories);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
%>
	<form action="AddPoint.jsp" method="POST">
        Имя: <input type="text" name="name" />
        <br />
        Широта:  <input type="text" name="lat">
		<br />
		Долгота: <input type="text" name="lng" />
        <br/>
        Категоря: <select name="category">
        <%
            DBCursor categoriesNames = ((DBCollection)request.getAttribute("categories")).find();
            while (categoriesNames.hasNext()){ // to fill the list of categories
        %>

            <option><%=categoriesNames.next().get("_id") %></option>

        <%}%>
        </select>
        <br/>
        <textarea rows="10" cols="45" name="description"></textarea>
        <br/>
		<input type="submit" value="Сохранить" />
	</form>

	
	<%
		if (request.getParameter("lat") != "" && request.getParameter("lng") != "" && request.getParameter("lat") != null && request.getParameter("lng") != null
                                                                                        && request.getParameter("name") != "" && request.getParameter("name") != null 
                                                                                        
                                                                                        && request.getParameter("description") != "" && request.getParameter("description") != null ) {

            LatLng lng1 = new LatLng(Double.parseDouble(request.getParameter("lat")), Double.parseDouble(request.getParameter("lng")));
			Point p1 = new Point(lng1);
			Feature f1 = new Feature(p1);

			try{
                MongoClient client = (MongoClient)request.getAttribute("client");
                DB db = client.getDB("gis");
                DBCollection points = db.getCollection("points");

	    		Object o = com.mongodb.util.JSON.parse(f1.toString());
	    		BasicDBObject geo = (BasicDBObject)o;
                geo.append("category", request.getParameter("category"));
                geo.append("name", request.getParameter("name"));
                geo.append("description", request.getParameter("description"));                
                                                            
                points.insert(geo);
	    		
	    		UpdateGeoJsonFile.update(client);
	    		
	    		                

	    	} catch (Exception e) {
	    		e.printStackTrace();
	    	}	
					
		}
	
	
	
	%>
		
	
	
</body>
</html>